package com.feng.dt.controller;

import com.feng.common.exception.ValidationException;
import com.feng.common.web.WebCommonResponse;
import com.feng.common.web.WebCommonResponseBuilder;
import com.feng.dt.model.entity.ScheduleJob;
import com.feng.dt.service.ScheduleJobService;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author Lee
 */
@RestController
@RequestMapping("/schedule/job")
public class ScheduleJobController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ScheduleJobController.class);

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ScheduleJobService scheduleJobService;

    @PostMapping(value = "/save.do")
    public WebCommonResponse<Void> save(@RequestBody ScheduleJob scheduleJob){
        try {
            scheduleJobService.save(scheduleJob);
        } catch (ValidationException e) {
            return new WebCommonResponseBuilder<Void>().fail(e.getErrorCode().toString(),e.getMessage()).build();
        }
        return new WebCommonResponseBuilder<Void>().success().build();
    }

    @PostMapping(value = "/start.do")
    public WebCommonResponse<Void> start(@RequestBody List<Integer> jobIds){
        scheduleJobService.startBatch(jobIds);
        return new WebCommonResponseBuilder<Void>().success().build();
    }

    @PostMapping(value = "/pause.do")
    public WebCommonResponse<Void> pauseBatch(@RequestBody List<Integer> jobIds){
        try {
            scheduleJobService.pauseJob(jobIds);
        } catch (ValidationException e) {
            return new WebCommonResponseBuilder<Void>().fail(e.getErrorCode().toString(),e.getMessage()).build();
        }
        return new WebCommonResponseBuilder<Void>().success().build();
    }

    @PostMapping(value = "/delete.do")
    public WebCommonResponse<Void> delete(@RequestBody List<Integer> jobIds) {
        try {
            scheduleJobService.deleteJob(jobIds);
            scheduleJobService.deleteBatch(jobIds);
        } catch (ValidationException e) {
            return new WebCommonResponseBuilder<Void>().fail(e.getErrorCode().toString(),e.getMessage()).build();
        }
        return new WebCommonResponseBuilder<Void>().success().build();
    }

    @PostMapping(value = "/update.do")
    public WebCommonResponse<Void> update(@RequestBody ScheduleJob scheduleJob) {
        try {
            scheduleJobService.updateJob(scheduleJob);
        } catch (ValidationException e) {
            return new WebCommonResponseBuilder<Void>().fail(e.getErrorCode().toString(),e.getMessage()).build();
        }
        return new WebCommonResponseBuilder<Void>().success().build();
    }

    @PostMapping(value = "/resume.do")
    public WebCommonResponse<Void> resume(@RequestBody List<Integer> jobIds) {
        try {
            scheduleJobService.resumeBatch(jobIds);
        } catch (ValidationException e) {
            return new WebCommonResponseBuilder<Void>().fail(e.getErrorCode().toString(),e.getMessage()).build();
        }
        return new WebCommonResponseBuilder<Void>().success().build();
    }
}
